﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities.Enums;

namespace Entities
{
    /// <summary>
    /// Interview class
    /// </summary>
    public class Interview : BaseEntity
    {
        public Interview()
            : base(-1)
        {
            this.questions = new List<Question>();
            this.State = InterviewState.Draft;
        }

        public Interview(Interview src)
            : base(src.ID)
        {
            this.questions = new List<Question>(src.Questions);
        }

        public Interview(int ID, string Title, int Author_id, InterviewState state, List<Question> questions)
            : base(ID)
        {
            this.Title = Title;
            this.questions = questions;
            this.State = state;
        }

        public int AuthorID { get; set; }
        public string Title { get; set; }
        public InterviewState State { get; set; }

        private List<Question> questions;
        /// <summary>
        /// Questions list
        /// </summary>
        public List<Question> Questions
        {
            get
            {
                return this.questions;
            }
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException("Question list cannot be null");
                }
                //if (value.Count == 0)
                //{
                //    throw new ArgumentException("Question list cannot be empty");
                //}
                this.questions = value;
            }
        }

        /// <summary>
        /// Add question method
        /// </summary>
        /// <param name="quest">Question to add</param>
        public void AddQuestion(Question quest)
        {
            quest.ChgState = EntityState.New;
            this.Questions.Add(quest);
        }

        /// <summary>
        /// Remove question method
        /// </summary>
        /// <param name="id">Id of the question to remove</param>
        public void RemoveQuestion(int id)
        {
            foreach (var quest in this.Questions.Where((qs) => (qs.ID == id)))
            {
                quest.ChgState = EntityState.Deleted;
            }
            this.Questions.RemoveAll((qs) => (qs.ID == id));
        }

        /// <summary>
        /// Edit question method
        /// </summary>
        /// <param name="id">Id of the question to edit</param>
        /// <param name="newQuestion">New question</param>
        public void EditQuestion(int id, Question newQuestion)
        {
            var item = this.Questions.FirstOrDefault((px) => (px.ID == id));
            if (item != null)
            {
                item = newQuestion;
                item.ChgState = EntityState.Changed;
            }
            else
            {
                throw new IndexOutOfRangeException();
            }
        }

        public bool IsValidId(int id)
        {
            return this.Questions.FirstOrDefault((px) => (px.ID == id)) == null; // Weak
        }
    }
}
